import { FormationType, FormationConfig, FormationEffect } from '../types';

export class Formation {
  private static readonly FORMATIONS: Record<FormationType, FormationConfig> = {
    [FormationType.JI_XING]: {
      probabilities: [0.6, 0.2, 0.2],
      frontReduction: 0.2,
      backBonus: 0.1,
      positions: [1, 4, 5]  // 前排1个，后排2个
    },
    [FormationType.YAN_XING]: {
      probabilities: [0.35, 0.35, 0.3],
      frontReduction: 0.1,
      backBonus: 0.3,
      positions: [0, 1, 4]  // 前排2个，后排1个
    },
    [FormationType.YI_ZI]: {
      probabilities: [0.333, 0.333, 0.334],
      frontReduction: 0.08,
      backBonus: 0,
      positions: [0, 1, 2]  // 前排3个
    }
  };

  constructor(private type: FormationType) {}

  getFormationEffect(position: number): FormationEffect {
    const config = Formation.FORMATIONS[this.type];
    const isFrontline = position < 3;
    
    return {
      damageReduction: isFrontline ? config.frontReduction : 0,
      damageBonus: !isFrontline ? config.backBonus : 0,
      targetProbability: this.getTargetProbability(position)
    };
  }

  getValidPositions(): number[] {
    return Formation.FORMATIONS[this.type].positions;
  }

  private getTargetProbability(position: number): number {
    const config = Formation.FORMATIONS[this.type];
    const index = this.getValidPositions().indexOf(position);
    return index >= 0 ? config.probabilities[index] : 0;
  }

  isValidPosition(position: number): boolean {
    return this.getValidPositions().includes(position);
  }
}
